package crm.tools;

import java.io.IOException;
import java.io.OutputStream;

import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

public class MyFastJsonHttpMessgaeConverter extends FastJsonHttpMessageConverter {

	private SerializeFilter serializeFilter = null;

	@SuppressWarnings("deprecation")
	@Override
	protected void writeInternal(Object obj, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException {
		if(serializeFilter != null){
			OutputStream out = outputMessage.getBody();
			// 如果注入了过滤器，则使用过滤器进行处理
			String text = JSON.toJSONString(obj, getSerializeFilter() , getFeatures());
			byte[] bytes = text.getBytes(getCharset());
			out.write(bytes);
		}else{
			// 如果没有映射配置，我们就调用父类的方法。
			super.writeInternal(obj, outputMessage);
		}
	}

	public SerializeFilter getSerializeFilter() {
		return serializeFilter;
	}

	public void setSerializeFilter(SerializeFilter serializeFilter) {
		this.serializeFilter = serializeFilter;
	}

}
